package jmetal.qualityIndicator.calculating;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.StringTokenizer;

import jmetal.core.Problem;
import jmetal.core.Solution;
import jmetal.core.SolutionSet;
import jmetal.qualityIndicator.QualityIndicator;

public class ComputeIndicator {
	
	
	public static void main(String[] args) throws ClassNotFoundException, 
	SecurityException, IOException, NoSuchMethodException, InstantiationException, 
	IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		
		//=====修改此处
		String algName = "NSGAIII";

	    String[] probName = {"jmetal.problems.DTLZ.DTLZ1",
	    		"jmetal.problems.DTLZ.DTLZ2",
	    		"jmetal.problems.DTLZ.DTLZ3",
	    		"jmetal.problems.DTLZ.DTLZ4"};
		
	    Integer[] numberOfVar = {4,9,9,9,9,9,19};
	    
		for (int probNum = 0; probNum <probName.length; probNum++) {
			Class<?> dtlz = Class.forName(probName[probNum]);
			Constructor<?> constr = dtlz.getConstructor(String.class,Integer.class,Integer.class);
			//======修改此处
			Problem problem = (Problem) constr.
					newInstance("Real",numberOfVar[probNum]+5,5);

			String pfDirect = "input_data/new_pf/" + problem.getName() + "_M" + problem.getNumberOfObjectives() + ".dat";
			System.out.println(pfDirect);
			QualityIndicator indicators = new QualityIndicator(problem, pfDirect);

			String outDirect = "output_data/"+algName+"/" + problem.getName() + "/" + problem.getNumberOfObjectives() + "D_IGD"
					+ ".dat";
			FileOutputStream output = new FileOutputStream(new File(outDirect));
			System.out.println(outDirect);
			for (int i = 0; i < 30; i++) {
				String path = "output_data/"+algName+"/" + problem.getName() + "/FUN_" + problem.getNumberOfObjectives() + "D_"
						+ i;
				System.out.println(path);
				
				//======修改此处
				SolutionSet population = setPopulation(path, 126);

				String out = "Run_" + i + ": " + indicators.getIGD(population) + "\n";
				output.write(out.getBytes());

				// String outDirect =
				// "results/NSGAIII/"+problem.getName()+"/"+problem.getNumberOfObjectives()+"M_"+i+".log";
				// Logger logger_ = Configuration.logger_;
				// FileHandler fileHandler_ = new FileHandler(outDirect);
				// logger_.addHandler(fileHandler_);
				// logger_.info("Quality indicators");
				// logger_.info("Hypervolume: " +
				// indicators.getHypervolume(population));
				// logger_.info("GD : " + indicators.getGD(population));
				// logger_.info("IGD : " + indicators.getIGD(population));
				// logger_.info("Spread : " + indicators.getSpread(population));
				// logger_.info("Epsilon : " +
				// indicators.getEpsilon(population));
			}
			output.close();
		}
	}
	
	private static SolutionSet setPopulation(String path,int size) {


		SolutionSet population = new SolutionSet(size);

		try {
			// Open the file
			FileInputStream fis = new FileInputStream(path);
			InputStreamReader isr = new InputStreamReader(fis);
			BufferedReader br = new BufferedReader(isr);

			int numberOfObjectives = 0;
			String aux = br.readLine();
			int i = 0;
			while (aux != null&&i<size) {
				StringTokenizer st = new StringTokenizer(aux);
				int j = 0;
				numberOfObjectives = st.countTokens();
				Solution solution = new Solution(numberOfObjectives);
				while (st.hasMoreTokens()) {
					double value = (new Double(st.nextToken())).doubleValue();
					solution.setObjective(j, value);
					
					// System.out.println("lambda["+i+","+j+"] = " + value) ;
					j++;
				}
				population.add(i, solution);
				aux = br.readLine();
				i++;
			}
			br.close();
		} catch (Exception e) {
			System.out
					.println("failed when reading for file: "
							+ "/" + path);
			e.printStackTrace();
		}

		return population;
	}

}
